<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 顾客信息
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <el-button type="primary" icon="el-icon-search" @click=handleAddgoods>添加</el-button>
      <el-table
          :data="tableData"
          border
          class="table"
          ref="multipleTable"

      >
        <el-table-column prop="Consumer_id" label="顾客编号" align="center" ></el-table-column>
        <el-table-column prop="Consumer_name" label="顾客名称" align="center"></el-table-column>
        <el-table-column prop="Consumer_address" label="顾客地址" align="center"></el-table-column>
        <el-table-column prop="Consumer_postcode" label="顾客邮编" align="center"></el-table-column>
        <el-table-column prop="Consumer_phone" label="顾客电话" align="center"></el-table-column>
        <el-table-column label="操作" width="300" align="center">
          <template slot-scope="scope">
            <el-button
                type="text"
                icon="el-icon-edit"
                @click="handlebuyGoods(scope.row)"
            >更新</el-button>
            <el-button
                type="text"
                icon="el-icon-delete"
                class="red"
                @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 添加弹出框 -->
    <el-dialog title="添加" :visible.sync="addVisible" width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="顾客编号">
          <el-input v-model="tempgoodsData.consumer_id"></el-input>
        </el-form-item>
        <el-form-item label="顾客名称">
          <el-input v-model="tempgoodsData.consumer_name"></el-input>
        </el-form-item>
        <el-form-item label="顾客地址">
          <el-input v-model="tempgoodsData.consumer_address"></el-input>
        </el-form-item>
        <el-form-item label="顾客邮编">
          <el-input v-model="tempgoodsData.consumer_postcode"></el-input>
        </el-form-item>
        <el-form-item label="顾客电话">
          <el-input v-model="tempgoodsData.consumer_phone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="addgoods">确 定</el-button>
            </span>
    </el-dialog>

    <el-dialog title="更新" :visible.sync="buyVisible" width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="顾客编号">
          <el-input v-model="orderData.consumer_id"></el-input>
        </el-form-item>
        <el-form-item label="顾客名称">
          <el-input v-model="orderData.consumer_name"></el-input>
        </el-form-item>
        <el-form-item label="顾客地址">
          <el-input v-model="orderData.consumer_address"></el-input>
        </el-form-item>
        <el-form-item label="顾客邮编">
          <el-input v-model="orderData.consumer_postcode"></el-input>
        </el-form-item>
        <el-form-item label="顾客电话">
          <el-input v-model="orderData.consumer_phone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
                <el-button @click="buyVisible = false">取 消</el-button>
                <el-button type="primary" @click="updateGoods">确 定</el-button>
            </span>
    </el-dialog>
  </div>


</template>

<script>
import {
  getcustomer,
  fetchupdatecustomer,
  addcustomer, customerdelete
} from '../index';
export default {
  name: 'basetable',
  data() {
    return {
      query: {
        address: '',
        name: '',
        pageIndex: 1,
        pageSize: 10
      },
      tableData: [],
      tempgoodsData:{},
      orderData:{},
      multipleSelection: [],
      delList: [],
      addVisible: false,
      buyVisible: false,
      pageTotal: 0,
      form: {},
      idx: -1,
      id: -1
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      getcustomer().then(res =>{console.info(res)

        this.tableData=res.data.value
        console.log(this.tableData)
      })
    },
    //更新操作
    handlebuyGoods(row){
      this.orderData = row;
      this.buyVisible = true;
    },
    // 更新成功
    updateGoods() {
      this.buyVisible = false;
      this.$message.success(`更新成功`);

      fetchupdatecustomer(this.orderData).then(res =>{console.info(res)
        console.log(this.orderData)
      })
    },
    //删除操作
    handleDelete(row){
      this.tempgoodsData = row
      customerdelete(this.tempgoodsData).then(res=>{console.info(res)
        console.log(this.tempgoodsData)
        console.log(res)
        window.location.reload()
      })
    },

    delAllSelection() {
      const length = this.multipleSelection.length;
      let str = '';
      this.delList = this.delList.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' ';
      }
      this.$message.error(`删除了${str}`);
      this.multipleSelection = [];
    },
    // 编辑操作
    handleAddgoods(){
      this.addVisible = true;
    },
    addgoods() {
      this.addVisible = false;
      this.$message.success(`添加成功`);
      addcustomer(this.tempgoodsData).then(res =>{console.info(res)
        console.log(this.tempgoodsData)
        console.log(res)
        window.location.reload()
      })
    },
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
